/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;


/**
 *
 * @author apple
 */
public class Client extends JPanel implements Runnable{

    /**
     * @param args the command line arguments
     */
    BufferedReader br;
    PrintWriter pw;
    String command;
    Socket client;
    String result_text;
    StringBuffer history_input = new StringBuffer();
    private boolean started = false;
	private JTextField input;
	private JLabel result;    
	private JButton new_game;
	private JLabel history;

    Client(Socket socket){
    	this.client = socket;
    	buildGUI(); 
    	connect();
    }
    
	@Override
	public void run() {
		// TODO Auto-generated method stub
		JFrame frame = new JFrame();
		frame.setContentPane(this);
		frame.setSize(30, 20);
	    frame.pack();
	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    frame.setVisible(true);
		
	}

	private void buildGUI() {
		// TODO Auto-generated method stub
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		new_game = new JButton("New Game");	
		new_game.setEnabled(false);
		result = new JLabel();		
		result.setSize(30, 15);
		input = new JTextField(20);
		input.setEnabled(false);
		history = new JLabel("History: ");	
		
		new_game.setAlignmentX(CENTER_ALIGNMENT);
		result.setAlignmentX(CENTER_ALIGNMENT);
		input.setAlignmentX(CENTER_ALIGNMENT);
		history.setAlignmentX(CENTER_ALIGNMENT);
		
		new_game.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try {
					if(started){
						sendCommand(client, "new_game");
						input.setEnabled(true);
						history_input.delete(0, history_input.length());
						history.setText("");
					}
					
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			
		});
		
		input.addActionListener(new MakeAGuess());

		add(result);			
		add(input);					
		add(history);

		add(new_game);
	}
    
    private class MakeAGuess implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			if(arg0.getSource() == input){

		    		 String guess = input.getText();
		    		 
		    		 if(!guess.equals("")){
			    		 command = guess;

			             try {
							sendCommand(client, command);
							input.setText("");
							guess = history_input.append(guess).append(" ").toString();
							history.setText(guess);
							
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
		    		 }

			}
			
		}
    	
    }
    
    private void connect(){

			// TODO Auto-generated method stub
    		//started = true;
			try {
        		    		
		        br = new BufferedReader(new InputStreamReader(client.getInputStream()));
		        result_text = br.readLine();
		        showResults(result_text);  

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
    }
	
    public void sendCommand(Socket s, String command) throws IOException {

        PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
        
        pw.println(command);
        
        result_text = br.readLine();
            	
    	showResults(result_text);   
        
    }

	public void showResults(final String new_result){
			
			SwingUtilities.invokeLater(new Runnable(){
	            @Override
	            public void run() {
	            	
	            	result.setText(new_result);    
	            	
	            	if(new_result.equals("Welcome to Hangman Game")){
	            		started = true;
	            		new_game.setEnabled(true);
	            	}	            		            	
	    		
	            }        		
			
			});
	
		}


}
